package tema4;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="TablasDeMultiplicarServlet", urlPatterns={"/tema4/tablasDeMultiplicar"})
public class TablasDeMultiplicarServlet extends HttpServlet {
    
    protected void procesaPeticion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();        
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TablasDeMultiplicar</title>");
            out.println("<link rel=\"stylesheet\" title=\"standar\" type=\"text/css\" href=\"/EjemploBasicoJavaEE/standar.css\">");
            out.println("</head>");
            out.println("<body>");
            
            int multiplicador = Integer.parseInt(request.getParameter("multiplicador"));
            int factor = Integer.parseInt(request.getParameter("factor"));
            int columnas = Integer.parseInt(request.getParameter("columnas"));          
            if(multiplicador<0 || factor<=0 || columnas<=0)
                out.println("<h2>Has insertado unos valores absurdos!!</h2>");
            
            out.println("<h1>Tabla de Multiplicar del: "+multiplicador+"</h1>");
            int[] tabla = calculaTablaDeMultiplicar(multiplicador,factor);
            pintaTablaDeMultiplicar(out, multiplicador, tabla, columnas);
            
        } catch (java.lang.NumberFormatException eParseo) {
            Logger.getLogger(TablasDeMultiplicarServlet.class.getName()).log(Level.SEVERE,
                    "Parametros del formulario erroneos", eParseo);
            out.println("<h2>Has insertado unos valores no numéricos!!</h2>");
        } finally {
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }

    private int[] calculaTablaDeMultiplicar(int multiplicador, int factor) {
        int[] tabla = new int[factor+1];
        for(int f=0;f<=factor;f++)
            tabla[f] = multiplicador * f;
        return tabla;
    }
    
    
    private void pintaTablaDeMultiplicar(PrintWriter out,int multiplicador,int[] tabla,int columnas) {
        int filas = (tabla.length+1)/columnas;
        int cuenta = 0;
        
        out.println("<table align=\"center\" border=\"1\">");
        out.println("<tr>");
        for(int fila=0;fila<=filas;fila++){
            out.print("<tr>");
            for(int columna=0;columna<columnas;columna++){
                out.print("<td>"+multiplicador+"x"+cuenta+" = "+tabla[cuenta]+"</td>");
                cuenta++;
            }
            out.println("</tr>");
        }
        out.println("</table>");
    }

}
